package cn.kgc.shiro02.controller;

import cn.kgc.shiro02.entity.User;
import cn.kgc.shiro02.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/7/28
 * @Description:
 */

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("login")
    public Boolean login(String username,String password){

        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        subject.login(usernamePasswordToken);
        return subject.isAuthenticated();
    }

    @GetMapping
    public List<User> getList(){
        return userService.list();
    }


    @RequestMapping("delete")
    public Boolean delete(Integer id){

        return userService.removeById(id);
    }
}
